package com.hnkywl.sts.dao.kcgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.xsgl.Lld;
import com.hnkywl.sts.entity.xsgl.Lldmx;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:14
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class LldDao extends SimpleHibernateTemplate<Lld, Long> {
    public Page<Lld> findLldList(String zt, Date kdrqStart, Date kdrqEnd, Long flgsId, String flgsJc, int pageNo, int pageSize) {
        Page<Lld> page = new Page<Lld>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("flgs", "flgs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("flbm", "flbm", CriteriaSpecification.LEFT_JOIN)
                .createAlias("flr", "flr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("thck", "thck", CriteriaSpecification.LEFT_JOIN)
                .createAlias("llgs", "llgs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("llbm", "llbm", CriteriaSpecification.LEFT_JOIN)
                .createAlias("llr", "llr", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("lldh").as("lldh"))
                        .add(Property.forName("flrq").as("flrq"))
                        .add(Property.forName("flgs.id").as("flgs_id"))
                        .add(Property.forName("flgs.jc").as("flgs_jc"))
                        .add(Property.forName("flbm.id").as("flbm_id"))
                        .add(Property.forName("flbm.jc").as("flbm_jc"))
                        .add(Property.forName("flr.id").as("flr_id"))
                        .add(Property.forName("flr.xm").as("flr_xm"))
                        .add(Property.forName("thck.id").as("thck_id"))
                        .add(Property.forName("thck.mc").as("thck_mc"))
                        .add(Property.forName("kdslzh").as("kdslzh"))
                        .add(Property.forName("kdjezh").as("kdjezh"))
                        .add(Property.forName("flslzh").as("flslzh"))
                        .add(Property.forName("fljezh").as("fljezh"))
                        .add(Property.forName("llgs.id").as("llgs_id"))
                        .add(Property.forName("llgs.jc").as("llgs_jc"))
                        .add(Property.forName("llbm.id").as("llbm_id"))
                        .add(Property.forName("llbm.jc").as("llbm_jc"))
                        .add(Property.forName("llr.id").as("llr_id"))
                        .add(Property.forName("llr.xm").as("llr_xm"))
                        .add(Projections.sqlProjection("kdrq as kdrq", new String[]{"kdrq"}, new Type[]{Hibernate.DATE}))
                        .add(Property.forName("kdr").as("kdr"))
                        .add(Projections.sqlProjection("this_.zt as zt", new String[]{"zt"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("bz").as("bz"))
                        .add(Property.forName("lastUpdated"))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (kdrqStart != null) {
            criteria.add(Restrictions.gt("kdrq", kdrqStart));
        }
        if (kdrqEnd != null) {
            criteria.add(Restrictions.lt("kdrq", kdrqEnd));
        }
        if (flgsId != null) {
            criteria.add(Restrictions.eq("flgs.id", flgsId));
        }
        if (StringUtils.isNotEmpty(zt)) {
            criteria.add(Restrictions.eq("zt", zt));
        }
        RegionDao.createDataRegion(criteria);
        Page list = this.findByCriteria(page, criteria);
        return list;
    }


    public List<Map> findLldmxList(Long lldId) {
        Criteria criteria = getSession().createCriteria(Lldmx.class)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cgd", "cgd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jzfs", "jzfs", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("kh.id").as("kh.id"))
                        .add(Property.forName("kh.khbm").as("kh.khbm"))
                        .add(Property.forName("kh.kkdsl").as("kkdsl"))
                        .add(Property.forName("kh.wsdj").as("wsdj"))
                        .add(Property.forName("kh.chd").as("chd"))
                        .add(Property.forName("kh.jz").as("jz"))
                        .add(Property.forName("cgd.cgdh").as("cgdh"))
                        .add(Property.forName("wzgg.id").as("wzgg.id"))
                        .add(Property.forName("wzgg.code").as("wzgg.code"))
                        .add(Property.forName("wzgg.name").as("wzgg.name"))
                        .add(Property.forName("wzgg.ggxh").as("wzgg.ggxh"))
                        .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                        .add(Property.forName("wzgg.th").as("wzgg.th"))
                        .add(Property.forName("cd.id").as("cd.id"))
                        .add(Property.forName("cd.z").as("cd.z"))
                        .add(Property.forName("kdjs").as("kdjs"))
                        .add(Property.forName("kdsl").as("kdsl"))
                        .add(Property.forName("kddj").as("kddj"))
                        .add(Property.forName("jzfs.id").as("jzfs.id"))
                        .add(Property.forName("jzfs.z").as("jzfs.z"))
                        .add(Property.forName("fljs").as("fljs"))
                        .add(Property.forName("flsl").as("flsl"))
                        .add(Property.forName("flje").as("flje"))
                        .add(Property.forName("fldj").as("fldj"))
                        .add(Property.forName("llbz").as("llbz"))
                        .add(Property.forName("kdje").as("kdje"))
                ).add(Restrictions.eq("lld.id", lldId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }


    public List<Map> findLldmxListByKh(Long khId) {
        Criteria criteria = getSession().createCriteria(Lldmx.class)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jzfs", "jzfs", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Projections.sqlProjection("kh1_.id as kh_id", new String[]{"kh_id"}, new Type[]{Hibernate.LONG}))
                        .add(Property.forName("kh.khbm").as("kh.khbm"))
                        .add(Property.forName("kh.kkdsl").as("kkdsl"))
                        .add(Property.forName("kh.wsdj").as("wsdj"))
                        .add(Property.forName("kh.chd").as("chd"))
                        .add(Property.forName("kh.jz").as("jz"))
                        .add(Property.forName("wzgg.id").as("wzgg.id"))
                        .add(Property.forName("wzgg.code").as("wzgg.code"))
                        .add(Property.forName("wzgg.name").as("wzgg.name"))
                        .add(Property.forName("wzgg.ggxh").as("wzgg.ggxh"))
                        .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                        .add(Property.forName("wzgg.th").as("wzgg.th"))
                        .add(Property.forName("cd.id").as("cd.id"))
                        .add(Property.forName("cd.z").as("cd.z"))
                        .add(Property.forName("kdjs").as("kdjs"))
                        .add(Property.forName("kdsl").as("kdsl"))
                        .add(Property.forName("kddj").as("kddj"))
                        .add(Property.forName("jzfs.id").as("jzfs.id"))
                        .add(Property.forName("jzfs.z").as("jzfs.z"))
                        .add(Property.forName("fljs").as("fljs"))
                        .add(Property.forName("flsl").as("flsl"))
                        .add(Property.forName("fldj").as("fldj"))
                        .add(Property.forName("flje").as("flje"))
                        .add(Property.forName("llbz").as("llbz"))
                ).add(Restrictions.eq("kh.id", khId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }
}


